
package com.piripo.services;
		

import com.piripo.entities.*;

import com.piripo.dao.MainDAOLocator;

import java.lang.reflect.Method;
import java.util.Collection;

import com.jrapid.entities.types.Time;
import com.jrapid.entities.types.TimeQuantity;
import com.jrapid.entities.types.TimeRange;
import com.jrapid.controller.Session;

import javax.servlet.jsp.el.ELException;
import org.apache.commons.el.ExpressionEvaluatorImpl;
import com.jrapid.services.ExpressionVariableResolver;


public class FunctionMapper extends com.jrapid.services.FunctionMapper {

	private static MainDAOLocator locator = MainDAOLocator.get();
		
	
	public static User find_User(Long id) {
		return locator.getUserDAO().findById(id);
	}	
	public static Collection<User> findAll_User() {
		return locator.getUserDAO().findAll();
	}
	public static Collection<User> findManyBy_User(String field, Object value) {
		return locator.getUserDAO().findManyBy(field, value);
	}
	public static User findOneBy_User(String field, Object value) {
		return locator.getUserDAO().findOneBy(field, value);
	}			
	
	public static Role find_Role(Long id) {
		return locator.getRoleDAO().findById(id);
	}	
	public static Collection<Role> findAll_Role() {
		return locator.getRoleDAO().findAll();
	}
	public static Collection<Role> findManyBy_Role(String field, Object value) {
		return locator.getRoleDAO().findManyBy(field, value);
	}
	public static Role findOneBy_Role(String field, Object value) {
		return locator.getRoleDAO().findOneBy(field, value);
	}			
	
	public static Login find_Login(Long id) {
		return locator.getLoginDAO().findById(id);
	}	
	public static Collection<Login> findAll_Login() {
		return locator.getLoginDAO().findAll();
	}
	public static Collection<Login> findManyBy_Login(String field, Object value) {
		return locator.getLoginDAO().findManyBy(field, value);
	}
	public static Login findOneBy_Login(String field, Object value) {
		return locator.getLoginDAO().findOneBy(field, value);
	}			
	
	public static ChangePassword find_ChangePassword(Long id) {
		return locator.getChangePasswordDAO().findById(id);
	}	
	public static Collection<ChangePassword> findAll_ChangePassword() {
		return locator.getChangePasswordDAO().findAll();
	}
	public static Collection<ChangePassword> findManyBy_ChangePassword(String field, Object value) {
		return locator.getChangePasswordDAO().findManyBy(field, value);
	}
	public static ChangePassword findOneBy_ChangePassword(String field, Object value) {
		return locator.getChangePasswordDAO().findOneBy(field, value);
	}			
	
	public static Location find_Location(Long id) {
		return locator.getLocationDAO().findById(id);
	}	
	public static Collection<Location> findAll_Location() {
		return locator.getLocationDAO().findAll();
	}
	public static Collection<Location> findManyBy_Location(String field, Object value) {
		return locator.getLocationDAO().findManyBy(field, value);
	}
	public static Location findOneBy_Location(String field, Object value) {
		return locator.getLocationDAO().findOneBy(field, value);
	}			
	
	public static Review find_Review(Long id) {
		return locator.getReviewDAO().findById(id);
	}	
	public static Collection<Review> findAll_Review() {
		return locator.getReviewDAO().findAll();
	}
	public static Collection<Review> findManyBy_Review(String field, Object value) {
		return locator.getReviewDAO().findManyBy(field, value);
	}
	public static Review findOneBy_Review(String field, Object value) {
		return locator.getReviewDAO().findOneBy(field, value);
	}			
	
	public static Experience find_Experience(Long id) {
		return locator.getExperienceDAO().findById(id);
	}	
	public static Collection<Experience> findAll_Experience() {
		return locator.getExperienceDAO().findAll();
	}
	public static Collection<Experience> findManyBy_Experience(String field, Object value) {
		return locator.getExperienceDAO().findManyBy(field, value);
	}
	public static Experience findOneBy_Experience(String field, Object value) {
		return locator.getExperienceDAO().findOneBy(field, value);
	}			
	
	public static BlogPost find_BlogPost(Long id) {
		return locator.getBlogPostDAO().findById(id);
	}	
	public static Collection<BlogPost> findAll_BlogPost() {
		return locator.getBlogPostDAO().findAll();
	}
	public static Collection<BlogPost> findManyBy_BlogPost(String field, Object value) {
		return locator.getBlogPostDAO().findManyBy(field, value);
	}
	public static BlogPost findOneBy_BlogPost(String field, Object value) {
		return locator.getBlogPostDAO().findOneBy(field, value);
	}			
	
	
	@SuppressWarnings("unchecked")
	public static Object fetchSet(Collection<Object> objs, String expr) throws ELException {
		ExpressionEvaluatorImpl el = new ExpressionEvaluatorImpl();
		Collection<Object> ret = new java.util.HashSet<Object>();
		java.util.Map<String, Object> map = new java.util.HashMap<String, Object>();
		ExpressionVariableResolver evr = new ExpressionVariableResolver(map);
		
		for (Object obj:objs) {
			map.put("obj", obj);
			Collection<Object> current = (Collection<Object>) el.evaluate("${obj." + expr + "}", Object.class, evr, new FunctionMapper());
			if (current != null) {
				ret.addAll(current);
			}
		}
		return ret;
	}
	
	public Method resolveFunction(String prefix, String name) {
		try {
			Method[] methods = FunctionMapper.class.getMethods();
			Method m = null;
			for (int i=0; i < methods.length; i++) {
				if (methods[i].getName().equals((prefix == null || "".equals(prefix) || "f".equals(prefix)) ? name : (name + "_" + prefix))) {
					m = methods[i];
					break;
				}
			}
			return m;
		} catch (SecurityException e) {
			return null;
		}
	}
}
	